<?php

/**
 * @author Kevin
 * @version 1.0
 * @created 12-d�c.-2011 19:51:41
 */
class League implements I_Persistent
{

	var $exist = false;
	/**
	 * Déclarations des données
	 */
	var $id = NULL;
	var $names = array();
	var $sport = NULL;


	/**
	 * Constructeur
	 * 
	 * @param pId
	 */
	function League($pId)
	{
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM leagues WHERE id = %d", $pId));
		
		$this->id = $pId;
		if($_row->id > 0){
			$this->sport = $_row->fk_sport;
			$this->exist = true;
				
			// recuperation des noms de la league
			$_row2 = $wpdb->get_results($wpdb->prepare("SELECT * FROM leaguenames WHERE fk_league = %d", $this->id));
			foreach($_row2 as $_name){
				$this->names[$_name->language] = $_name->value;
			}
		}
		else
		{
			// création du tableau des nom de compétition avec des valeurs vides.
			foreach(getLanguages() as $_language){
				$this->names[$_language->id] = "";
			}
		}
	}

	function create()
	{
		$this->createLeague();
		$this->createLeagueNames();
	}

	function createLeague()
	{
		global $wpdb;
			
		$queryTable = 'leagues';
		$queryData = array(
					'id' => $this->id, 
					'fk_sport' => $this->sport,
		);
		$queryDataFormat = array('%d','%d');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
	}

	function createLeagueNames()
	{
		global $wpdb;
		
		foreach(getLanguages() as $_language){
			$queryTable = 'leaguenames';
			$queryData = array(
						'fk_league' => $this->id,
						'language' => $_language->id,
						'value' => $this->names[$_language->id]
			);
			$queryDataFormat = array('%d','%s','%s');
				
			$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
		}
	}

	function delete()
	{
	}


	function getCountBetsAvailable()
	{
		global $wpdb;
		return 
		$wpdb->get_var(
			$wpdb->prepare(
				// request
				"SELECT count(*)
				FROM bets b 
				JOIN events e ON b.fk_event = e.id 	
				JOIN publications p ON e.id = p.fk_concretid			
				WHERE e.fk_league = %d	
				AND	(	( p.date > '%s')
						 OR
						( p.date = '%s' AND p.hour > '%s'	)
				)",
				
				// params		
				$this->id,
				date("Y-m-d"),
				date("Y-m-d"),
				date("H:i:s")		
			)
		);
	}

	/**
	 * 
	 * @param pDate
	 */
	function getCountBetsAvailableForDate($pDate)
	{
		global $wpdb;
		return 
		$wpdb->get_var(
			$wpdb->prepare(
				// request
				"SELECT count(*)
				FROM bets b 
				JOIN events e ON b.fk_event = e.id 			
				JOIN publications p ON e.id = p.fk_concretid					
				WHERE e.fk_league = %d	
				AND	p.date = '%s'
				AND p.hour > '%s'",
	
				// params
				$this->id,
				$pDate,
				date("H:i:s")		
			)
		);
	}

	/**
	 * ====================== Récupération de la liste des events rattachés �
	 * cette league pour une date donnée ======================
	 * 
	 * @param pDate
	 */
	function getEvents($pDate)
	{
		global $wpdb;
		return 
		$wpdb->get_results(
			$wpdb->prepare(
				// request
				"SELECT e.id
				FROM events e 
				JOIN publications p ON e.id = p.fk_concretid				
				WHERE e.fk_league = %d
				AND p.date = '%s' AND p.hour > '%s'
				ORDER BY p.hour",
		
				// params
				$this->id,
				$pDate,
				date('H:i:s')
			)
		);
	}


	function getId()
	{
		return $this->id;
	}

	function save()
	{
		$this->saveLeague();
		$this->saveLeagueNames();
	}

	function saveLeague()
	{
		global $wpdb;
		
		$queryTable = 'leagues';
		$queryData = array( 'fk_sport' => $this->sport);
		$queryDataFormat = array('%d');
		$queryCondition = array("id" => $this->id);
		$queryConditionFormat = array('%d');
		
		$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
	}

	function saveLeagueNames()
	{
		global $wpdb;
		
		foreach(getLanguages() as $_language){
			$queryTable = 'leaguenames';
			$queryData = array( 'value' => $this->names[$_language->id]	);
			$queryDataFormat = array('%s');
			$queryCondition = array("fk_league" => $this->id, "language" => $_language->id);
			$queryConditionFormat = array('%d', '%s');
				
			$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
		}
	}

	/**
	 * 
	 * @param value
	 */
	function setExist($value)
	{
		$this->exist = $value;
	}

	function getExist(){
		return $this->exist;
	}
	
	/**
	 * 
	 * @param pLang
	 * @param pName
	 */
	public function setName($lang, $name)
	{
	    $this->names[$lang] = $name;
	}
	
	public function getName($lang)
	{
	   return $this->names[$lang];
	}
	
	/**
	*
	* @param sport
	*/
	public function setSport($sport)
	{
	    $this->sport = $sport;
	}
	
	public function getSport()
	{
	    return $this->sport;
	}

}
?>